package org.apache.commons.graph.export

import kotlinx.io.Buffer
import kotlinx.io.asInputStream
import kotlinx.io.readString
import org.apache.commons.graph.utils.Assertions
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.OutputStream
import java.io.Writer


@Throws(GraphExportException::class)
fun<V, E, T : AbstractExporter<V, E, T>> AbstractExporter<V,E,T>.to(outputFile: File) {
    try {
        to(FileOutputStream(Assertions.checkNotNull(outputFile, "Impossibe to export the graph in a null file")))
    } catch (e: FileNotFoundException) {
        throw RuntimeException(e)
    }
}

@Throws(GraphExportException::class)
fun<V, E, T : AbstractExporter<V, E, T>> AbstractExporter<V,E,T>.to(outputStream: OutputStream) {
    val buffer = Buffer()
    to(buffer)
    buffer.asInputStream().transferTo(outputStream)
    //to(OutputStreamWriter(Assertions.checkNotNull(outputStream, "Impossibe to export the graph in a null stream")))
}

@Throws(GraphExportException::class)
fun<V, E, T : AbstractExporter<V, E, T>> AbstractExporter<V,E,T>.to(writer: Writer) {
    val buffer = Buffer()
    to(buffer)
    writer.write(buffer.readString())
}
